package com.example.work_03;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AIDL extends AppCompatActivity {
    IMyAidlInterface mStub;
    EditText et1,et2;
    TextView tv;
    Button get;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aidl);
        et1=(EditText) findViewById(R.id.et1);
        et2=(EditText) findViewById(R.id.et2);
        tv=(TextView) findViewById(R.id.tv04);
        get=(Button) findViewById(R.id.btn_01);
        Intent intent=new Intent();
        intent.setAction("com.example.work_03.MyService");
        intent.setPackage("co.example.work_03.");
        ServiceConnection sc=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mStub=IMyAidlInterface.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,sc,BIND_AUTO_CREATE);
        get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(et1.getText().toString());
                int b=Integer.parseInt(et2.getText().toString());
                int value;
                try {
                    value=mStub.add(a,b);
                    tv.setText(String.valueOf(value));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
